﻿using System.Data.SqlClient;
using InvestmentIntelligence.DbModel.Models;
using System;
using System.Collections.Generic;
using System.Linq;

namespace InvestmentIntelligence.Data.Repository.EFImplementation
{
    public class CorporateEventRepository : IIPRepository<CorporateEvent>, ICorporateEventRepository
    {
        public DateTime? GetLastDate(int key)
        {
            return Get(dc => dc.Max(c => (DateTime?)c.Date));
        }

        public List<T> GetListBySecurity<T>(Func<IQueryable<CorporateEvent>, IEnumerable<T>> f, int securityId)
        {
            return GetList(dc => f(dc.Where(c => c.SecurityId == securityId)));
        }

        

        public void RemoveBySecurities(List<int> securityIds, DateTime startDate, DateTime endDate)
        {
            var securityIdsParameter = string.Join(", ", securityIds);
            ExecuteSqlCommand(DB.SqlCommandBuilder.ComposeDelete(GetTableName(), "SecurityId in (" + securityIdsParameter + ") and [Date]>= @StartDate  and [Date]<= @EndDate"),
                                new SqlParameter("@StartDate", startDate), new SqlParameter("@EndDate", endDate));
        }
    }
}